﻿/*
 * RM2 Character Generator
 * Copyright (C) 2009 James Culshaw
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of 
 * the GNU General Public License as published by the Free Software Foundation; either version 2 
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; 
 * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
using System;

namespace RMCharGen.Tables
{
    /// <summary>
    /// Models 15.1.2 Stat Gain Table pg. 38 Character Law & Campaign Law
    /// </summary>
    internal class StatGain
    {

        private int[,] _gains = {{-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
                                 {-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                                 {-1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2},
                                 {-1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3},
                                 {-1, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4},
                                 {-1, 0, 0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6},
                                 {-1, 0, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8},
                                 {-1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10},
                                 {-1, 1, 2, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12},
                                 {-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}};

        private int _current;
        private int _potential;

        private int _column;

        public StatGain(int current,
                        int potential)
        {
            this._current = current;
            this._potential = potential;

            this._column = DetermineColumn();
        }

        private static int DetermineRow(int roll)
        {
            int row = 20;

            if (roll < 100)
            {
                row = (int)Math.Ceiling(((double)roll) / 5);
                row -= 1;
            }

            row = Math.Max(0, row);

            return row;
        }

        private int DetermineColumn()
        {
            int difference = this._potential - this._current;
            int column = 0;

            difference = Math.Max(0, difference);
            
            switch (difference)
            {
                case 0:
                case 1:
                case 2:
                case 3:
                    column = difference;
                    break;
                case 4:
                case 5:
                    column = 4;
                    break;
                case 6:
                case 7:
                    column = 5;
                    break;
                case 8:
                case 9:
                    column = 6;
                    break;
                case 10:
                case 11:
                    column = 7;
                    break;
                case 12:
                case 13:
                case 14:
                    column = 8;
                    break;
                default:
                    column = 9;
                    break;   
            }

            return column;
        }

        public int GetGain(int roll)
        {
            int gain = 0;

            int row = DetermineRow(roll);

            gain = this._gains[this._column, row];

            if (gain == -1)
            {
                gain = roll * -2;
            }

            return gain;
        }
    }
}
